package sr.codemg.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sr.codemg.config.RedisUtil;
import sr.codemg.entity.SysMenus;
import sr.codemg.entity.SysRoleMenu;
import sr.codemg.entity.SysRoles;
import sr.codemg.mapper.SysMenusMapper;
import sr.codemg.mapper.SysRoleMenuMapper;
import sr.codemg.mapper.SysRolesMapper;
import sr.codemg.service.SysRolesService;
import sr.codemg.utils.Constants;
import sr.codemg.utils.ResponseCode;
import sr.codemg.utils.StaticFunc;
import sr.codemg.vm.Base;
import sr.codemg.vm.Selects;
import sr.codemg.vm.SysRolesEdit;
import sr.codemg.vm.Tables;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 权限信息
 **/
@Service
public class SysRolesServiceImpl implements SysRolesService {

    @Autowired
    SysRolesMapper sysRolesMapper;
    @Autowired
    SysRoleMenuMapper sysRoleMenuMapper;
    @Autowired
    SysMenusMapper sysMenusMapper;
    @Autowired
    RedisUtil redisUtil;

    @Override
    public SysRoles get(String id) {
        return sysRolesMapper.get(id);
    }

    @Override
    public SysRolesEdit getEdit(String id) {
        SysRoles sysRoles = get(id);
        SysRoleMenu roleMenu = new SysRoleMenu();
        roleMenu.setRoleId(id);
        List<SysRoleMenu> data = sysRoleMenuMapper.findByFields(roleMenu);
        List<String> menuIds = new ArrayList<>();
        data.forEach(item -> {
            menuIds.add(item.getMenuId());
        });
        SysRolesEdit result = new SysRolesEdit();
        result.setId(sysRoles.getId());
        result.setName(sysRoles.getName());
        result.setMenuIds(menuIds);
        return result;
    }

    @Override
    public Tables findList(Map<String, Object> params) {
        StaticFunc.page(params);
        List<SysRoles> data = sysRolesMapper.findList(params);
        Integer count = sysRolesMapper.count(params);
        return new Tables(data, count);
    }

    @Override
    @Transactional
    public Base.Response add(SysRolesEdit entity) {
        sysRolesMapper.add(entity);
        List<String> menuIds = entity.getMenuIds();
        menuIds.forEach(item -> {
            SysRoleMenu roleMenu = new SysRoleMenu();
            roleMenu.setRoleId(entity.getId());
            roleMenu.setMenuId(item);
            SysMenus menus = sysMenusMapper.get(item);
            if (menus == null) {
                return;
            }
            roleMenu.setButtonIds(menus.getBtnId());
            sysRoleMenuMapper.add(roleMenu);
        });

        return ResponseCode.getBase(Constants.SUCC);
    }

    @Override
    @Transactional
    public Base.Response update(SysRolesEdit entity) {
        List<String> menuIds = entity.getMenuIds();
        menuIds.forEach(item -> {
            SysRoleMenu roleMenu = sysRoleMenuMapper.get(entity.getId(), item);
            if (roleMenu == null) {
                roleMenu = new SysRoleMenu();
                roleMenu.setRoleId(entity.getId());
                roleMenu.setMenuId(item);
                SysMenus menus = sysMenusMapper.get(item);
                if (menus == null) {
                    return;
                }
                roleMenu.setButtonIds(menus.getBtnId());
                sysRoleMenuMapper.add(roleMenu);
            }
        });
        sysRolesMapper.update(entity);
        return ResponseCode.getBase(Constants.SUCC);
    }

    @Override
    @Transactional
    public Base.Response delete(String id) {
        sysRoleMenuMapper.delete(id);
        sysRolesMapper.delete(id);
        return ResponseCode.getBase(Constants.SUCC);
    }

    @Override
    public Selects.Response select(Selects.Request req) {
        req.configPage();
        List<Selects.Select> data = sysRolesMapper.select(req);
        Selects.Response res = new Selects.Response();
        res.setData(data);
        return res;
    }
}

